package com.fingard.dsp.bank.directbank.wjrb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
/**
 * @author: liuxb
 * @create: 2019-12-17 14:19
 * @version: 1.0
 **/
public class WJRB01Balance extends WJRB01Base implements IBalance {
    /**
     * 吴江农商行查询余额
     *
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //余额查询返回给ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            RetAtsBalDetail detail = new RetAtsBalDetail();
            //赋值通用参数
            WJRB01Header bankHeader = new WJRB01Header();
            bankHeader.TrsCode = "B2EActBalQry";//银行交易码
            bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
            bankHeader.ERPTrsTimestamp = p_req7506.reqHeader.transTime;//指令请求时间
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入SDEBANK01通用的头部节点
            writeCommonHeader2(xmlWriter, bankHeader);
            //写入发送给银行报文体
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeStartElement("List");
            while (p_req7506.readRecord()) {// 循环调用readRecord()读取一个请求明细（还没读过就返回true）
                xmlWriter.writeStartElement("Map");
                xmlWriter.writeElementString("AcNo", p_req7506.readedDetail.actNum);//账号
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndDocument();

            // 发送给银行获得应答报文
            String[] resultBank = sendToBank(xmlWriter.toXmlString(), bankHeader);
            //模拟银行返回
//            String s = "<Message>\n" +
//                    "\t\t<Head>\n" +
//                    "\t\t\t<IBSReturnCode>000000</IBSReturnCode>\n" +
//                    "\t\t\t<IBSReturnMsg>银企交易成功！</IBSReturnMsg>\n" +
//                    "\t\t</Head>\n" +
//                    "\t\t<Body>\n" +
//                    "\t\t\t<List>\n" +
//                    "\t\t\t\t<Map>\n" +
//                    "\t\t\t\t\t<ReturnCode>000000</ReturnCode>\n" +
//                    "\t\t\t\t\t<ReturnMsg>响应1成功！</ReturnMsg>\n" +
//                    "\t\t\t\t\t<AcNo>05618800005460</AcNo>\n" +
//                    "\t\t\t\t\t<Currency>CNY</Currency>\n" +
//                    "\t\t\t\t\t<Balance>12000.00</Balance>\n" +
//                    "\t\t\t\t\t<AvailBal>10000.00</AvailBal>\n" +
//                    "\t\t\t\t</Map>\n" +
//                    "\t\t\t\t<Map>\n" +
//                    "\t\t\t\t\t<ReturnCode>000000</ReturnCode>\n" +
//                    "\t\t\t\t\t<ReturnMsg>响应2成功！</ReturnMsg>\n" +
//                    "\t\t\t\t\t<AcNo>05618800008196</AcNo>\n" +
//                    "\t\t\t\t\t<Currency>CNY</Currency>\n" +
//                    "\t\t\t\t\t<Balance>12000.76</Balance>\n" +
//                    "\t\t\t\t\t<AvailBal>10000.76</AvailBal>\n" +
//                    "\t\t\t\t</Map>\n" +
//                    "\t\t\t</List>\n" +
//                    "\t\t</Body>\n" +
//                    "</Message>\n";
//            String[] resultBank = new String[]{"", s};
            if (resultBank[0].length() == 0) {// 成功响应
                StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                XmlTextReader tmpBkReader = loadBankRespXmlReader(resultBank[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                if (tmpBkReader != null) {// 返回报文的内容不为空
                    String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                    if ("000000".equals(IBSReturnCode)) {
                        while (tmpBkReader.read("/Message/Body/List/Map")) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            String returnCode = tmpBkReader.readedXElement.getSingleText("ReturnCode");
                            tmpRetDetail.respCode = "000000".equals(returnCode) ? "0" : "-1";
                            tmpRetDetail.respInfo = tmpBkReader.readedXElement.getSingleText("ReturnMsg");
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("AcNo");//帐号
                            tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("Currency"));
                            tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("Balance");//帐户余额
                            tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("AvailBal");//可用余额
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);// 把信息提取完毕的对象装载入返回给ats的对象中
                        }
                    } else if (IBSReturnCode != null && "999999".equals(IBSReturnCode)) {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(IBSReturnMsg);
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo("银企直联通讯机校验错误，或者通讯失败！");
                    }

                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(resultBank[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
